package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户分页")
    @PostMapping("page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<UserVo>> selectPage(@PathVariable("pageNum") Integer pageNum,
                                                       @PathVariable("pageSize") Integer pageSize,
                                                       @RequestBody UserDto userDto) {
        log.info("用户分页查询，pageNum={},pageSize={},userDto={}", pageNum, pageSize, userDto);
        PageBean<UserVo> pageBean = userService.selectPage(userDto, pageNum, pageSize);
        return ResponseResult.success(pageBean);
    }

    @ApiOperation("用户添加")
    @PutMapping
    public ResponseResult<Void> addUser(@RequestBody UserDto userDto) {
        log.info("用户添加，userDto={}", userDto);
        userService.addUser(userDto);
        return ResponseResult.success();
    }


    @ApiOperation("用户修改")
    @PatchMapping
    public ResponseResult<Void> updateUser(@RequestBody UserDto userDto) {
        log.info("用户修改，userDto={}", userDto);
        userService.updateUser(userDto);
        return ResponseResult.success();
    }



    @ApiOperation("删除用户")
    @DeleteMapping("remove/{userIds}")
    public ResponseResult<Void> removeUser(@PathVariable("userIds") Long userId) {
        log.info("删除用户，userIds={}", userId);
        userService.deleteUserById(userId);
        return ResponseResult.success();
    }



    @ApiOperation("启用或禁用用户")
    @PutMapping("is-enable/{id}/{status}")
    public ResponseResult<Void> isEnable(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        log.info("启用或禁用用户，id={},status={}", id, status);
        userService.isEnable(id, status);
        return ResponseResult.success();
    }


    @ApiOperation("用户列表")
    @PostMapping("list")
    public ResponseResult<List<UserVo>> list() {
        return ResponseResult.success(userService.list());
    }



    @ApiOperation("密码重置")
    @PostMapping("reset-passwords/{userId}")
    public ResponseResult<Void> resetPasswords(@PathVariable("userId") Long userId) {
        log.info("密码重置，userId={}", userId);
        userService.resetPasswords(userId);
        return ResponseResult.success();
    }
}
